import numeral from 'numeral'

import {
  keepDecimalPlaces,
  ThousandthNumber,
  GlobalTimeformat,
  GlobalDateformat
} from '@/utils/common'
/**
 * [Format return result]
 * @param {String} type
 * @param {*} value
 * @returns {String}
 */
export function formatFunction (type, value, data) {
  let tempData = null
  switch (type) {
    case 'number':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        try {
          if (data.fieldsNewFormat.fieldsNewFormatType === 'number') {
            if (data.fieldsNewFormat.option === 'regular') {
              var temp = `${keepDecimalPlaces(value, data.fieldsNewFormat.decimals)}`
              tempData = ThousandthNumber(temp)
            } else if (data.fieldsNewFormat.option === 'abbreviate') {
              if (value >= 1000000000) {
                tempData = `${keepDecimalPlaces(value, data.fieldsNewFormat.decimals, '/', 1000000000)}g`
              } else if (value >= 1000000) {
                tempData = `${keepDecimalPlaces(value, data.fieldsNewFormat.decimals, '/', 1000000)}m`
              } else {
                tempData = `${keepDecimalPlaces(value, data.fieldsNewFormat.decimals, '/', 1000)}k`
              }
            } else {
              tempData = `${keepDecimalPlaces(value, data.fieldsNewFormat.decimals)}`
            }
          } else if (data.fieldsNewFormat.fieldsNewFormatType === 'percentage') {
            tempData = `${keepDecimalPlaces(value, data.fieldsNewFormat.decimals, '*', 100)}%`
          } else if (data.fieldsNewFormat.fieldsNewFormatType === 'currency') {
            if (typeof (data.fieldsNewFormat.prefix) === 'undefined') {
              data.fieldsNewFormat.prefix = '$'
            }
            if (data.fieldsNewFormat.option === 'regular') {
              var tempp = `${keepDecimalPlaces(value, data.fieldsNewFormat.decimals)}`
              var templ = ThousandthNumber(tempp)
              tempData = `${data.fieldsNewFormat.prefix}${templ}`
            } else if (data.fieldsNewFormat.option === 'abbreviate') {
              if (value >= 1000000000) {
                tempData = `${data.fieldsNewFormat.prefix}${keepDecimalPlaces(value, data.fieldsNewFormat.decimals, '/', 1000000000)}g`
              } else if (value >= 1000000) {
                tempData = `${data.fieldsNewFormat.prefix}${keepDecimalPlaces(value, data.fieldsNewFormat.decimals, '/', 1000000)}m`
              } else {
                tempData = `${data.fieldsNewFormat.prefix}${keepDecimalPlaces(value, data.fieldsNewFormat.decimals, '/', 1000)}k`
              }
            } else {
              tempData = `${data.fieldsNewFormat.prefix}${keepDecimalPlaces(value, data.fieldsNewFormat.decimals)}`
            }
            tempData = tempData.replace(`${data.fieldsNewFormat.prefix}-`, `-${data.fieldsNewFormat.prefix}`)
          } else if (data.fieldsNewFormat.fieldsNewFormatType === 'custom') {
            tempData = numeral(value).format(data.fieldsNewFormat.custom)
          } else {
            tempData = value
          }
        } catch (error) {
          tempData = value
        }
      }
      break
    case 'number-1k':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = `${keepDecimalPlaces(value, 2, '/', 1000)}k`
      }
      break
    case 'number-0':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 0)
      }
      break
    case 'number-0.0':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 1)
      }
      break
    case 'number-0.00':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 2)
      }
      break
    case 'number-0.000':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 3)
      }
      break
    case 'number-0.0000':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 4)
      }
      break
    case 'number-0.00000':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 5)
      }
      break
    case 'number-0.00percent':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = `${keepDecimalPlaces(value, 2, '*', 100)}%`
      }
      break
    case 'currency-0':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 0)
      }
      break
    case 'currency-0.0':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 1)
      }
      break
    case 'currency-0.00':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 2)
      }
      break
    case 'currency-0.000':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 3)
      }
      break
    case 'currency-0.0000':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = keepDecimalPlaces(value, 4)
      }
      break
    case 'currency-dollar0':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = `$${keepDecimalPlaces(value, 0)}`
      }
      break
    case 'currency-dollar0.0':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = `$${keepDecimalPlaces(value, 1)}`
      }
      break
    case 'currency-dollar0.00':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = `$${keepDecimalPlaces(value, 2)}`
      }
      break
    case 'currency-dollar0.000':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = `$${keepDecimalPlaces(value, 3)}`
      }
      break
    case 'currency-dollar0.0000':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = `$${keepDecimalPlaces(value, 4)}`
      }
      break
    case 'currency-euro0':
      if (isNaN(Number(value))) {
        tempData = value
      } else {
        tempData = `€${keepDecimalPlaces(value, 0)}`
      }
      break
    case 'date':
      if (data.businessType === 'date') {
        tempData = GlobalDateformat(value, data.fieldsFormat, data.showbymonday)
      } else {
        let timezoneCheckedData
        if (data.fieldsList) {
          timezoneCheckedData = data.fieldsList.find(ii=>ii.alias===data.field) && data.fieldsList.find(ii=>ii.alias===data.field).timezoneChecked
        } else {
          timezoneCheckedData = data.timezoneChecked
        }
        tempData = GlobalTimeformat(value, null, data.fieldsFormat, data.showbymonday, timezoneCheckedData)
      }
      break
    case 'date-YYYY-MM-DD':
      tempData = GlobalDateformat(value, 'YYYY-MM-DD', data.showbymonday)
      break
    case 'date-MM-DD-YY':
      tempData = GlobalDateformat(value, 'MM-DD-YY', data.showbymonday)
      break
    case 'date-MMM_YYYY':
      tempData = GlobalDateformat(value, 'MMM YYYY', data.showbymonday)
      break
    case 'date-YY-MM-DD':
      tempData = GlobalDateformat(value, 'YY-MM-DD', data.showbymonday)
      break
    case 'date-YYYY':
      tempData = GlobalDateformat(value, 'YYYY', data.showbymonday)
      break
    case 'datetime-MMM_YYYY':
      tempData = GlobalTimeformat(value, null, 'MMM YYYY', data.showbymonday)
      break
    case 'datetime-YYYY-MM-DD':
      tempData = GlobalTimeformat(value, null, 'YYYY-MM-DD', data.showbymonday)
      break
    case 'datetime-YY-MM-DD':
      tempData = GlobalTimeformat(value, null, 'YY-MM-DD', data.showbymonday)
      break
    case 'datetime-MM-DD-YY':
      tempData = GlobalTimeformat(value, null, 'MM-DD-YY', data.showbymonday)
      break
    case 'datetime-YYYY-MM-DD_HH.mm':
      tempData = GlobalTimeformat(value, 'HH:mm', 'YYYY-MM-DD', data.showbymonday)
      break
    case 'datetime-YYYY-MM-DD_HH.mm.ss':
      tempData = GlobalTimeformat(value, 'HH:mm:ss', 'YYYY-MM-DD', data.showbymonday)
      break
    case 'datetime-YYYY-MM-DD_hh.mm.ss_am.pm':
      tempData = GlobalTimeformat(value, 'hh:mm:ss a', 'YYYY-MM-DD', data.showbymonday)
      break
    case 'datetime-YYYY-MM-DD_hh.mm.ss_AM.PM':
      tempData = GlobalTimeformat(value, 'hh:mm:ss A', 'YYYY-MM-DD', data.showbymonday)
      break
    case 'datetime-MM-DD-YYYY_hh.mm_am.pm':
      tempData = GlobalTimeformat(value, 'hh:mm a', 'MM-DD-YYYY', data.showbymonday)
      break
    case 'boolean-1_0':
      if (value) {
        tempData = 1
      } else {
        tempData = 0
      }
      break
    case 'boolean-OK_NA':
      if (value) {
        tempData = 'OK'
      } else {
        tempData = 'NA'
      }
      break
    case 'boolean-On_Off':
      if (value) {
        tempData = 'On'
      } else {
        tempData = 'Off'
      }
      break
    case 'boolean-True_False':
      if (value) {
        tempData = 'True'
      } else {
        tempData = 'False'
      }
      break
    case 'boolean-Active_InActive':
      if (value) {
        tempData = '启用'
      } else {
        tempData = '已停用'
      }
      break
    case 'boolean-Yes_No':
      if (value) {
        tempData = 'Yes'
      } else {
        tempData = 'No'
      }
      break
    case 'boolean-Enable_Disable':
      if (value) {
        tempData = 'Enable'
      } else {
        tempData = 'Disable'
      }
      break
    case 'text-ticket_status':
      if (value === '1') {
        tempData = '订单待接收'
      }
      if (value === '2') {
        tempData = '订单已接收'
      }
      if (value === '3') {
        tempData = '物料分拣中'
      }
      if (value === '4') {
        tempData = '物料配送中'
      }
      if (value === '5') {
        tempData = '已完成'
      }
      if (value === '6') {
        tempData = '已驳回'
      }
      if (value === '7') {
        tempData = '已撤销'
      }
      break
    case 'text-agv_status':
      if (value === '0') {
        tempData = '准备中'
      }
      if (value === '1') {
        tempData = '已派发'
      }
      if (value === '2') {
        tempData = '任务中'
      }
      if (value === '3') {
        tempData = '已完成'
      }
      if (value === '4') {
        tempData = '异常'
      }
      break
    case 'text-ddj_status':
      if (value === '0') {
        tempData = '准备中'
      }
      if (value === '1') {
        tempData = '执行中'
      }
      if (value === '2') {
        tempData = '已完成'
      }

      break
    default:
      tempData = value
      break
  }
  return tempData
}
